package com.leetcode.根据数据结构分类.数组;

/**
 * @author: xiaomi
 * @date: 2021/2/28
 * @description: 896. 单调数列
 * https://leetcode-cn.com/problems/monotonic-array/
 */
public class A_896_单调数列 {
    public static void main(String[] args) {

    }

    public boolean isMonotonic(int[] A) {
        int len = A.length;
        if (len < 3) {
            return true;
        }
        //不会越界
        int compare = A[0] - A[len - 1];
        int lastValue = A[0];
        if (compare == 0) {
            //此时需要保证所有的值都相等
            for (int i = 1; i < len; i++) {
                if (A[i] != lastValue) {
                    return false;
                }
            }
            return true;
        } else if (compare < 0) {
            //此时需要 递增
            for (int i = 1; i < len; i++) {
                if (A[i] < lastValue) {
                    return false;
                }
                lastValue = A[i];
            }
            return true;
        } else {
            //此时需要 递减
            for (int i = 1; i < len; i++) {
                if (A[i] > lastValue) {
                    return false;
                }
                lastValue = A[i];
            }
            return true;
        }
    }
}
